let needLoadingRequestCount = 0;
let loadingTimer;
export default {
	getTime() {
		let year = new Date().getFullYear();
		let month = new Date().getMonth() + 1;
		let date = new Date().getDate();
		if (month < 10) month = "0" + month;
		if (date < 10) date = "0" + date;
		return year + "-" + month + "-" + date;
	},
	showTime(now) {
		let year = new Date(now).getFullYear();
		let month = new Date(now).getMonth() + 1;
		let date = new Date(now).getDate();
		if (month < 10) month = "0" + month;
		if (date < 10) date = "0" + date;
		return year + "-" + month + "-" + date;
	},
	//本周第一天；
	showWeekFirstDay(){
		let Nowdate = new Date();
		let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
		let M = Number(WeekFirstDay.getMonth()) + 1;
		if (M < 10) {
			M = "0" + M;
		}
		let D = WeekFirstDay.getDate();
		if (D < 10) {
			D = "0" + D;
		}
		console.log("sy"+ WeekFirstDay.getFullYear() + "-" + M + "-" + D)
		return WeekFirstDay.getFullYear() + "-" + M + "-" + D;
	},
	//本周最后一天
	showWeekLastDay(){
		let Nowdate = new Date();
		let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
		let WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000);
		let M = Number(WeekLastDay.getMonth()) + 1;
		if (M < 10) {
			M = "0" + M;
		}
		let D = WeekLastDay.getDate();
		if (D < 10) {
			D = "0" + D;
		}
		return WeekLastDay.getFullYear() + "-" + M + "-" + D;
	},
	//获得某月的天数：
	getMonthDays(myMonth) {
		let monthStartDate = new Date(new Date().getFullYear(), myMonth, 1);
		let monthEndDate = new Date(new Date().getFullYear(), myMonth + 1, 1);
		let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
		return days;
	},
	paramsSort(param) {
		var result = "";
		for (let name in param) {
			if (typeof param[name] != 'function') {
				result += "&" + name + "=" + encodeURI(param[name]);
			}
		}
		var newResult = result.substring(1);
		// console.log(JSON.stringify(newResult));
		return newResult.toLowerCase();
	},
	//计算两个经纬度的距离(千米)
	getDistance(lat1, lng1, lat2, lng2) {
		var radLat1 = lat1*Math.PI / 180.0;
		var radLat2 = lat2*Math.PI / 180.0;
		var a = radLat1 - radLat2;
		var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
		Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
		s = s *6378.137 ;// EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	},
	//加载弹窗
	showLoading(title = '', mask = true){
		if (needLoadingRequestCount === 0) {
			uni.showLoading({
				title,
				mask
			});
	 
			// 最长10s自动关闭
			loadingTimer = setTimeout(() => {
				if (needLoadingRequestCount > 0) {
					uni.hideLoading();
				}
			}, 10000);
		}
		 
		needLoadingRequestCount++;
	},
	//隐藏加载弹窗
	hideLoading(){
		if (needLoadingRequestCount <= 0) return;
		 
		needLoadingRequestCount--;
	 
		if (needLoadingRequestCount === 0) {
			loadingTimer && clearTimeout(loadingTimer);
			uni.hideLoading();
		}
	}
}